//获取应用实例
const app = getApp()


Page({
  data: {
    showPop:false,
    wantWorkArea: "",
    bannerList: [],
    tabs: ["全职", "兼职"],
    tabIndex: 1,


    jobType: 1,
    showFilter: false,

    showWorkDate: false,
    sWorkDate: -1, //选择工作日期
    sShift: -1, //选择班次

    showInterviewTime: false,
    interviewDates: [], //可选面试时间

    welfareList: [], //选择福利待遇

    resultPayType: [], //其他筛选
  },

  onLoad() {
    let that = this;
    this.getBannerLists();
    let res = wx.getStorageSync('location');
    let globalData = getApp().globalData;
    if (res) {
      globalData.longitude = res[0];
      globalData.latitude = res[1];
      globalData.address = res[2];
      globalData.addressTitle = res[3];

      this.setData({
        wantWorkArea: globalData.addressTitle
      })

    } else {
      let util = getApp().require("utils/util.js")
      util.getLocation().then(res => {
        globalData.longitude = res.longitude;
        globalData.latitude = res.latitude;

        util.searchPOI("", res.latitude, res.longitude).then(res => {
          globalData.addressTitle = res.data[0].title;
          that.setData({
            wantWorkArea: globalData.addressTitle
          })
        });
      });
    }

    const jobListView = this.selectComponent("#jobList");
    const data = this.getSelectData();
    jobListView.getJobList(data, false);
  },



  //下来刷新
  onPullDownRefresh() {
    const jobListView = this.selectComponent("#jobList");
    const data = this.getSelectData();
    jobListView.getJobList(data, false);
  },

  //上拉加载
  onReachBottom() {
    const jobListView = this.selectComponent("#jobList");
    const data = this.getSelectData();
    jobListView.getJobList(data, true);
  },



  //选择期望地点
  selectAddress: function (params) {
    let that = this;
    wx.navigateTo({
      url: '/pages/other/select_address/select_address',
      events: {
        selectAddress: function (res) {
          let address = getApp().globalData.addressTitle;
          if (address && address.length > 0) {
            that.setData({
              wantWorkArea: address
            });
          }
          that.submitFilter();
        }
      }
    })
  },

  getBannerLists() {
    let that = this;
    app.$http.getBannerList().then(res => {
      that.setData({
        bannerList: res
      });
    });
  },


  clickBanner(e) {
    const index = e.currentTarget.dataset.index;
    const data = this.data.bannerList[index];
    const navigation = data.navigation && JSON.parse(data.navigation);
    if (navigation && navigation.refType == 1) {
      if (data.refUrl) {
        wx.navigateTo({
          url: '/pages/webView/webView?refUrl='+encodeURIComponent(data.refUrl),
        })
      }
    } else if (navigation && navigation.refType == 2) {
      if (navigation.refId) {
        app.$routes.goTo(app.$routes.postDetail,{
          id:navigation.refId
        })
      }
    }
  },

  selectTab(e) {
    const tabIndex = e.detail.tabIndex;
    if (tabIndex == 0) {
      this.data.jobType = 1;
    } else {
      this.data.jobType = 2;
    }
    this.data.tabIndex = tabIndex;
    this.submitFilter();
  },

  showWorkTimeDialog() {
    const show = !this.data.showWorkDate;
    this.setData({
      showWorkDate: show,
      showInterviewTime: false,
      showFilter: show
    })
  },


  showInterviewTimeDialog() {
    const show = !this.data.showInterviewTime;
    this.setData({
      showInterviewTime: show,
      showWorkDate: false,
      showFilter: show,
    })
  },


  //选择工作时间
  selectWorkDateType(res) {
    this.setData({
      sWorkDate: res.detail.sWorkDate,
      sShift: res.detail.sShift,
    });
    if (!res.detail.reset) {
      this.submitFilter();
    }
  },


  //选择福利
  selectWelfare() {
    let that = this;
    wx.navigateTo({
      url: '/pages/main/index/select-welfare/select-welfare?welfareList=' + JSON.stringify(that.data.welfareList),
      events: {
        welfarePage: function (res) {
          that.setData({
            welfareList: res.data
          })
          that.submitFilter();
        }
      }
    })

  },


  //选择可面试时间
  selectInterviewTime(res) {
    this.setData({
      interviewDates: res.detail.interviewTime,
    });
    if (!res.detail.reset) {
      this.submitFilter();
    }
  },



  //选择其他页面
  selectOther() {
    let that = this;
    wx.navigateTo({
      url: '/pages/main/index/select-other/select-other?from=homePage&payTypeList=' + JSON.stringify(that.data.resultPayType),
      events: {
        homeSelectOtherPage: function (res) {
          that.setData({
            resultPayType: res.resultPayType
          });
          that.submitFilter();
        }
      }
    })
  },


  submitFilter() {
    this.setData({
      showFilter: false,
      showWorkDate: false,
      showInterviewTime: false,
    });
    const jobListView = this.selectComponent("#jobList");
    const data = this.getSelectData();
    jobListView.getJobList(data, false);
  },

  getSelectData() {
    return {
      interviewDates: this.data.interviewDates,
      jobType: this.data.jobType,
      settlementTypes: this.data.resultPayType,
      lat: app.globalData.latitude,
      lng: app.globalData.longitude,
      wantWorkArea: this.data.wantWorkArea,
      shiftType: this.data.sShift == -1 ? null : this.data.sShift,
      welfareIds: this.data.welfareList,
      workType: this.data.sWorkDate == -1 ? null : this.data.sWorkDate,
    }
  },


  //去认证
  goAuth(){
    this.closePop();
  },

  closePop(){
    this.setData({
      showPop:false
    })
  }
})